<?php

namespace App\Console\Commands\OpenShop\Ekb;

use App\Models\Ekb\EkbApplyHistoryModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopSignOrderModel;
use Illuminate\Console\Command;
use Illuminate\Support\Collection;

class EkbApplyHistoryGenerateCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'ekb:ekbApplyHistoryGenerate';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '易快报:生成已提交记录';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    const SHOP_CODE_GROUP = [
        "2602",
        "2148",
        "2458",
        "2926",
        "2970",
        "3098",
        "2811",
        "3111",
        "3050",
        "3144",
        "2846",
        "3042",
        "2162",
        "2877",
        "0902",
        "3082",
        "2839",
        "2691",
        "1800",
        "3136",
        "3137",
        "1811",
        "2812",
        "3048",
        "3109",
        "2738",
        "2759",
        "2172",
        "2610",
        "2184",
        "2455",
        "2486",
        "2741",
        "2730",
        "1856",
        "2355",
        "2725",
        "2761",
        "2742",
        "2440",
        "2540",
        "0970",
        "2684",
        "2254",
        "2371",
        "0379",
        "2638",
        "2802",
        "2618",
        "2664",
        "2785",
        "2533",
        "2653",
        "2570",
        "2499",
        "2747",
        "2722",
        "2316",
        "2607",
        "2898",
        "2693",
        "2775",
        "1625",
        "2794",
        "2583",
        "1961",
        "2662",
        "2399",
        "2304",
        "2560",
        "2654",
        "2753",
        "2771",
        "2715",
        "2387",
        "2773",
        "2634",
        "2706",
        "2523",
        "2783",
        "1562",
        "1987",
        "2825",
        "2751",
        "2285",
        "2284",
        "1700",
        "1990",
        "1950",
        "2501",
        "2204",
        "2837",
        "2632",
        "1777",
        "2852",
        "2170",
        "2838",
        "2180",
        "2661",
        "2816",
        "2748",
        "2947",
        "2979",
        "2913",
        "2833",
        "2850",
        "2879",
        "2602",
        "2921",
        "2128",
        "2842",
        "2249",
        "2401",
        "2772",
        "2687",
        "1881",
        "2962",
        "2789",
        "2950",
        "2202",
        "2848",
        "2952",
        "622",
        "2769",
        "2543",
        "2388",
        "2114",
        "2936",
        "2386",
        "2757",
        "2733",
        "2637",
        "2928",
        "2945",
        "2777",
        "2735",
        "2904",
        "2778",
        "2356",
        "3107",
        "2774",
        "2164",
        "2871",
        "2368",
        "1955",
        "2764",
        "2522",
        "1776",
        "0782",
        "2935",
        "3121",
        "2922",
        "2980",
        "2788",
        "2966",
        "2874",
        "2835",
        "2911",
        "2939",
        "2955",
        "2118",
        "2694",
        "2832",
        "2815",
        "2953",
        "2796",
        "2112",
        "2682",
        "2148",
        "2270",
        "2813",
        "2633",
        "2576",
        "2863",
        "2317",
        "2920",
        "2382",
        "2864",
        "2969",
        "2968",
        "2676",
        "2882",
        "2679",
        "3116",
        "1963",
        "2827",
        "2719",
        "3083",
        "1207",
        "2418",
        "0977",
        "2678",
        "2265",
        "2299",
        "2659",
        "2362",
        "2362",
        "2588",
        "1848",
        "2420",
        "2500",
        "2554",
        "2752",
        "2765",
        "2743",
        "1981",
        "1988",
        "2319",
        "1640",
        "1741",
        "2410",
        "2889",
        "1035",
        "2929",
        "2892",
        "2383",
        "2675",
        "2263",
        "2880",
        "2870",
        "2843",
        "2868",
        "1922",
        "2875",
        "2297",
        "2797",
        "2938",
        "2878",
        "2689",
        "2590",
        "2941",
        "2800",
        "1885",
        "1202",
        "2756",
        "2828",
        "2884",
        "1735",
        "2801",
        "2718",
        "2895",
        "2958",
        "2925",
        "2866",
        "2989",
        "2988",
        "2829",
        "0110",
        "1638",
        "0619",
        "3093",
        "2329",
        "2891",
        "2817",
        "2238",
        "2682",
        "2112",
        "2112",
        "2788",
        "3057",
        "2814",
        "2271",
        "2820",
        "1080",
    ];

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        collect(self::SHOP_CODE_GROUP)->each(function ($originalShopCode) {
            $shopCode = ltrim($originalShopCode, "0");
            $orderNo  = attributeGet(
                OpenShopSignOrderModel::query()->where("shopCode", $shopCode)->first(),
                "orderNo",
                ""
            );

            if ($orderNo && empty(EkbApplyHistoryModel::query()->where("orderNo", $orderNo)->first())) {
                EkbApplyHistoryModel::query()->insert([
                    "orderNo"       => $orderNo,
                    "source"        => EkbApplyHistoryModel::SOURCE_1,
                    "shopCode"      => $shopCode,
                    "refundAmount"  => 5000,
                    "content"       => json_encode([], JSON_UNESCAPED_UNICODE),
                    "flowId"        => "",
                    "approveResult" => json_encode([], JSON_UNESCAPED_UNICODE),
                    "status"        => 1,
                    "createdAt"     => time(),
                    "updatedAt"     => time()
                ]);
            } else {
                $this->getOutput()->warning("门店查询开店订单失败或已存在数据库中 : {$shopCode}");
            }
        });


        EkbApplyHistoryModel::query()->where("shopCode", 0)
                            ->chunkById(100, function (Collection $ekbApplyHistoryCollection) {
                                $ekbApplyHistoryCollection->each(function (EkbApplyHistoryModel $ekbApplyHistoryModel) {
                                    $signOrderModel = OpenShopSignOrderModel::query()->where("orderNo",
                                        $ekbApplyHistoryModel->orderNo)->first();
                                    if ($signOrderModel && $signOrderModel->shopCode) {
                                        $ekbApplyHistoryModel->shopCode = $signOrderModel->shopCode;
                                        $ekbApplyHistoryModel->save();
                                    }
                                });
                            });

        return 0;
    }
}
